package com.disruptor.demo.queue;

import com.disruptor.demo.factory.SeriesEventFactory;
import com.disruptor.demo.handler.SeriesDataEventHandler;
import com.disruptor.demo.model.SeriesData;
import com.disruptor.demo.model.SeriesDataEvent;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.WorkHandler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author <a href="mailto:18335553083@163.com">Mr_He</a>
 * @Copyright (c)</ b> HeC<br/>
 * @createTime 2018/8/2 8:28
 * @Description: 生产者定义
 */
@Component
public class SeriesDataEventQueueHelper extends BaseQueueHelper<SeriesData, SeriesDataEvent, SeriesDataEventHandler> implements InitializingBean {

    //队列长度
    private static final int QUEUE_SIZE = 1024;

    /**
     * 消费者数量
     */
    @Autowired
    private List<SeriesDataEventHandler> seriesDataEventHandlers;


    @Override
    protected int getQueueSize() {
        return QUEUE_SIZE;
    }

    @Override
    protected com.lmax.disruptor.EventFactory eventFactory() {
        return new SeriesEventFactory();
    }

    /**
     * 获得队列的消费者
     * @return
     */
    @Override
    protected WorkHandler[] getHandler() {
        int size = seriesDataEventHandlers.size();
        SeriesDataEventHandler[] paramEventHandlers = (SeriesDataEventHandler[]) seriesDataEventHandlers.toArray(new SeriesDataEventHandler[size]);
        return paramEventHandlers;
    }

    /**
     * 指定等待策略
     * @return
     */
    @Override
    protected WaitStrategy getStrategy() {
        return new BlockingWaitStrategy();
    }

    /**
     * 初始化bean
     */
    @Override
    public void afterPropertiesSet() {
        this.init("disruptor_series");
    }
}